﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Infrastructure.Diagnostics;

namespace TempEmployees.BLL.AnamReports
{
    public class AnamReportsParser
    {
        private List<AnamReport> AnamReports { get; set; }
        private List<AnamReport> ErroredReports { get; set; }
        private List<TempEmployees.DAL.AnamReport> ReportsToSubmit { get; set; }

        public AnamReportsParser()
        {
            AnamReports = new List<AnamReport>();
            ErroredReports = new List<AnamReport>();
        }

        public void SubmitReport(string reportPath)
        {
            ParseReport(reportPath);

            CreateReportsToSubmit();

            SubmitReports();
        }

        private void ParseReport(string reportPath)
        {
            Logger.Debug("Start Parse report: {0}", reportPath);
            if (!File.Exists(reportPath))
            {
                Logger.Debug("Report doesn't exist: {0}", reportPath);
            }
            try
            {
                using (StreamReader sr = new StreamReader(reportPath))
                {
                    string line;
                    string[] values;
                    AnamReport report;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.StartsWith(";")) continue;
                        Logger.Debug("parsing: {0}", line);
                        values = line.Split(',');
                        report = new AnamReport();
                        report.Line = line;
                        report.ID = values[0].Trim();
                        report.Date = values[1].Trim();
                        report.Hours = values[2].Trim();
                        report.Budget = values[3].Trim();
                        report.Salary = values[4].Trim();
                        AnamReports.Add(report);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void CreateReportsToSubmit()
        {
            ReportsToSubmit = new List<TempEmployees.DAL.AnamReport>();
            foreach (AnamReport report in AnamReports)
            {
                try
                {
                    string[] dateParts = report.Date.Split('/');
                    DateTime date = new DateTime(int.Parse(dateParts[1]), int.Parse(dateParts[0]), 1);

                    decimal hours = decimal.Parse(report.Hours);
                    decimal salary = decimal.Parse(report.Salary);

                    ReportsToSubmit.Add(TempEmployees.BLL.EntitiesExtensions.EAnamReport.CreateAnamReport(report.ID, date, hours, report.Budget, salary));
                }
                catch (Exception)
                {
                    ErroredReports.Add(report);
                }
            }
        }

        private void SubmitReports()
        {
            if (ReportsToSubmit.Count == 0)
            {
                Logger.Debug("no reports to submit");
            }
            else
            {
                Logger.Debug("number of reports to submit: {0}", ReportsToSubmit.Count);
                try
                {
                    TempEmployees.BLL.EntitiesExtensions.EAnamReport.SubmitReports(ReportsToSubmit);
                }
                catch (Exception e)
                {
                    Logger.Error("Exception in SubmitReport: {0}", Logger.ExceptionMessage(e));
                }
            }
            ReportErrors();
        }

        private void ReportErrors()
        {
            Logger.Info("Errors, count: {0}", ErroredReports.Count);
            StringBuilder builder = new StringBuilder();
            ErroredReports.ForEach((r) =>
            {
                if (builder.Length > 0) builder.Append(" | ");
                builder.Append(r.Line);
            });
            Logger.Debug("Errored lines: {0}", builder.ToString());
        }
    }
}
